__all__ = ["SQLBase"]

class SQLBase(object):
    """
    This object is used to create a common base for all database API objects.
    This ensures that all datbase APIs accept a consistent interface. This is
    here to ensure that we have a consistent base throughout all of our database
    APIs.
    """
    def __init__(self, *args):
        """
        Default constructor. Executed when this object is initiated.
        """
        self.connection = None
        self.cursor = None

    def execute(self, statement, *args):
        """
        Create a wraper to execute a command upon the SQL cursor.
        
        @param str statement An SQL statement to execute
        @param tuple args A list of arguments that will be injected into the SQL
                          statement.  
        """
        self.cursor.execute(statement, args)

    def fetchone(self):
        """
        Mimics the sqlite fetchone method which recides within a cursor object.
        Ensures that a single value is returned from the cursor object if only
        one object exists within the tuple from the query, otherwise it returns
        the query result

        @return mixed The result from the query command
        """
        objectResult = self.cursor.fetchone()
        if hasattr(objectResult, "__iter__"):
            if len(objectResult) == 1:
                return objectResult[0]
        return objectResult

    def fetchall(self):
        """
        Mimics the sqlite fetchall method which recides within a cursor object.
        Ensures that the true values are added to a list so that we don't need
        to index it if the value is only one item in length (e.g. item instead
        of (item,)...)

        @return list Attributes from which the query returned
        """
        trueValues = []
        for value in self.cursor.fetchall():
            if hasattr(value, "__iter__"):
                if len(value) > 1:
                    trueValues.append(value)
                else:
                    trueValues.append(value[0])
            else:
                trueValues.append(value)
        return trueValues

    def save(self):
        """
        Commits the database to harddrive so that we can load it if the server
        closes.

        @return bool Whether or not the database was saved
        """
        raise NotImplementedError("Database '%s' is an incomplete API" % 
                                  self.__class__.__name__)


    def clear(self, saveDatabase = True):
        """
        Deletes all instance within the tables of the databse.

        @param bool saveDatabase Optional value, if True, it will commit the
                                 database
        """
        raise NotImplementedError("Database '%s' is an incomplete API" % 
                                  self.__class__.__name__)

    def close(self):
        """
        Closes the connections so that no further queries can be made.
        """
        self.cursor.close()
        self.connection.close()